<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle" style="display: flex;">
        <!-- 搜索 -->
        <label class="el-form-item-label">机器code</label>
        <el-input v-model="query.robotCode" clearable placeholder="机器code" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">机器type</label>
        <el-input v-model="query.robotType" clearable placeholder="机器type" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">通讯地址</label>
        <el-input v-model="query.robotAddress" clearable placeholder="通讯地址" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">物理地址</label>
        <el-input v-model="query.storageId" clearable placeholder="物理地址" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <div style="display: flex;align-items: center;gap: 5px;">
          <rrOperation :crud="crud" />
          <el-button type="primary" icon="el-icon-aim" size="mini" class="filter-item" @click="openAGVDialog">指定AGV</el-button>
        </div>
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" :plain="true" />
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <!-- <el-table-column type="selection" width="55" /> -->
        <el-table-column prop="robotCode" label="机器code" />
        <el-table-column prop="robotType" label="机器type" />
        <el-table-column prop="robotAddress" label="通讯地址" />
        <el-table-column prop="storageId" label="物理地址" />
        <el-table-column prop="status" label="状态" />
      </el-table>
      <!--分页组件-->
      <pagination />
      <!-- AGV跨楼层 -->
      <el-dialog title="指定AGV做跨楼层搬运任务" :close-on-click-modal="false" :visible.sync="AGVDialog" width="500px" @close="closeAGVDialog">
        <el-form :model="AGVForm" label-width="120px">
          <el-form-item label="AGV列表">
            <!-- <el-input v-model="AGVForm.selectAGV" style="width: 250px;" /> -->
            <el-select v-model="AGVForm.selectAGV" style="width: 250px;" placeholder="请选择">
              <el-option
                v-for="item in AGVList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="closeAGVDialog">取 消</el-button>
          <el-button type="primary" :loading="btn.confirm" @click="confirmWork">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getAGVListAPI, AGVWorkAPI } from '@/api/work/mission'
import crudWcsAgv from '@/api/work/wcsAgv'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
// import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { id: null, robotCode: null, robotType: null, robotAddress: null, storageId: null, status: null, createBy: null, updateBy: null, createTime: null, updateTime: null }
export default {
  name: 'WcsAgv',
  components: { pagination, crudOperation, rrOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: 'WcsAgv', url: 'api/wcsAgv', idField: 'id', sort: 'id,desc', crudMethod: { ...crudWcsAgv },
      optShow: {
        add: false,
        edit: false,
        reset: true,
        del: false,
        download: true
      }
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'wcsAgv:add'],
        edit: ['admin', 'wcsAgv:edit'],
        del: ['admin', 'wcsAgv:del']
      },
      rules: {
      },
      queryTypeOptions: [
        { key: 'robotCode', display_name: '机器code' },
        { key: 'robotType', display_name: '机器type' },
        { key: 'robotAddress', display_name: '通讯地址' },
        { key: 'storageId', display_name: '物理地址' }
      ],
      AGVDialog: false,
      AGVForm: {
        selectAGV: ''
      },
      AGVList: [],
      btn: {
        confirm: false
      }
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    openAGVDialog() {
      this.getAGVList()
      this.$nextTick(() => {
        this.AGVDialog = true
      })
    },
    closeAGVDialog() {
      this.AGVForm = {
        selectAGV: ''
      }
      this.AGVList = []
      this.AGVDialog = false
    },
    async getAGVList() {
      const res = await getAGVListAPI()
      // console.log(res)
      this.AGVList = res.data.map(item => {
        return {
          value: item,
          label: item
        }
      })
    },
    async confirmWork() {
      try {
        this.btn.confirm = true
        const res = await AGVWorkAPI(this.AGVForm.selectAGV)
        // console.log(res)
        if (res.code === 200) {
          this.$message.success(res.message)
          this.AGVForm = {
            selectAGV: ''
          }
          this.AGVList = []
          this.AGVDialog = false
          this.crud.toQuery()
        } else {
          this.AGVForm = {
            selectAGV: ''
          }
          this.$message.warning(res.message)
        }
      } finally {
        this.btn.confirm = false
      }
    }
  }
}
</script>

<style scoped>

</style>
